package com.ht.medicaltreatment.activity

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.viewpager.widget.ViewPager
import com.blankj.utilcode.util.GsonUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.CentrifugeFragmentAdapter
import com.ht.medicaltreatment.bean.AllCentrifugeBean
import com.ht.medicaltreatment.bean.CentrifugeStateBean
import com.ht.medicaltreatment.fragment.CentrifugeFragment
import com.ht.medicaltreatment.viewmodel.CentrifugeMainViewModel
import com.stx.xhb.androidx.XBanner
import com.stx.xhb.androidx.transformers.Transformer
import com.xgst.lib.utils.showToast
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.activity_centrifuge_layout.imgNext
import kotlinx.android.synthetic.main.activity_centrifuge_layout.imgPre
import kotlinx.android.synthetic.main.activity_centrifuge_layout.viewPager
import kotlinx.android.synthetic.main.activity_centrifuge_layout.xbanner

//离心机
class CentrifugeActivity:BaseActivity(), View.OnClickListener, ViewPager.OnPageChangeListener {
    val model by viewModels<CentrifugeMainViewModel>()

    private var list = mutableListOf<AllCentrifugeBean>()
    private var currentPostion = 0
    var map = mutableMapOf<Int,CentrifugeStateBean>()
    private var downTimeDisposable: Disposable? = null
    private val pagerAdapter = CentrifugeFragmentAdapter(this)
    private val fragments = arrayListOf<CentrifugeFragment>()

    override fun getLayoutId() = R.layout.activity_centrifuge_layout

    override fun isViewStup() = true

    override fun init() {
        model.bindActivity(this)
        setTitle("离心")

        showProgress()
        xbanner.setShowIndicatorOnlyOne(false)
        xbanner.setPageTransformer(Transformer.Scale)
        xbanner.setAllowUserScrollable(false)

        xbanner.loadImage(object : XBanner.XBannerAdapter{
            override fun loadBanner(banner: XBanner?, model: Any?, view: View?, position: Int) {
                val item = list[position]
                if(item.name.isNotEmpty()){
                    Glide.with(this@CentrifugeActivity)
                        .load(R.drawable.icon_lxj_top)
                        .apply(RequestOptions().centerCrop())
                        .into(view as ImageView)
                }
            }
        })
        xbanner.setOnPageChangeListener(this)


        viewPager.adapter = pagerAdapter
        viewPager.isUserInputEnabled = false

        imgNext.setOnClickListener(this)
        imgPre.setOnClickListener(this)

        initData()
    }

    fun initData() {

        //查询是否有未完成的离心机订单
        model.centrifugeState({
            map.clear()
            it.forEach {
                map[it.centrifugeId] = it
            }

            //查询所有离心机
            model.findAllCentrifuge({

                if(it == null || it.size == 0){
                    dismissProgress()
                    return@findAllCentrifuge
                }
                if(it.size == 2){
                    it.add(AllCentrifugeBean())
                }
                xbanner.setBannerData(it)
                list.clear()
                list.addAll(it)

                if(fragments.size > 0){
                    fragments[currentPostion].changeBean(GsonUtils.toJson(it[currentPostion]))
                    dismissProgress()
                    return@findAllCentrifuge
                }

                fragments.clear()
                it.forEach {
                    val fragment = CentrifugeFragment()
                    val bundle = Bundle().apply {
                        putString("bean", GsonUtils.toJson(it))
                    }
                    fragment.arguments = bundle
                    fragments.add(fragment)
                }
                pagerAdapter.setData(fragments)

                dismissProgress()
            })
        })
    }

    override fun onClick(v: View?) {
        when(v){
            imgPre -> {
                if(currentPostion > 0){
                    currentPostion--
                    xbanner.setBannerCurrentItem(currentPostion,true)
                }else{
                    showToast("已经是第一个了")
                }
            }

            imgNext -> {
                if(list.size <= currentPostion){
                    return
                }
                if(currentPostion+1 >= list.size){
                    showToast("已经是最后一个了")
                    return
                }
                val name = list.get(currentPostion+1).name
                if(name.isNotEmpty()){
                    currentPostion++
                    xbanner.setBannerCurrentItem(currentPostion,true)
                }else{
                    showToast("已经是最后一个了")
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if(fragments.size > currentPostion){
            fragments[currentPostion].sampleData()
        }
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        viewPager.setCurrentItem(position,true)
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onDestroy() {
        super.onDestroy()
        downTimeDisposable?.dispose()
    }
}